<?php
declare(strict_types=1);

namespace App\Service;

use App\Helper\Log;
use App\Helper\Util;
use Hyperf\DbConnection\Db;

/**
 * 人物服务
 */
class PersonService 
{
    /**
     * [__construct description] 构造函数
     */
    public function __construct()
    {}
    /**
     * 获取人物-分页
     * [getPersonPage description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function getPersonPage($params)
    {
        $currentPage = $params['currentPage'];
        $pageSize = $params['pageSize'];
        $where = [['is_del', '=', 0],['status', '=', 1]];
        $offset = ($currentPage - 1) * $pageSize;
        $columns = [
            'id',
            'name',
            'image'
        ];
        $datas = Db::table('vote_person')
            ->select($columns)
            ->where($where)
            ->offset($offset)
            ->limit($pageSize)
            ->orderBy('sort', 'desc')
            ->get()
            ->toArray();
        return $datas;
    }
    /**
     * 保存人物数据
     * [personSave description]
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function personSave($params)
    {
        if(!empty($params['id'])){
            $save_data = [
                'updated_at'=>time()
            ];
            if(!empty($params['name'])){
                $save_data['name'] = $params['name'];
            }
            if(!empty($params['image'])){
                $save_data['image'] = $params['image'];
            }
            if(!empty($params['introduction'])){
                $save_data['introduction'] = $params['introduction'];
            }
            if(!empty($params['sort'])){
                $save_data['sort'] = $params['sort'];
            }
            if(isset($params['status'])){
                $save_data['status'] = $params['status'];
            }
            Db::table('vote_person')
                ->where(['id'=>$params['id']])
                ->update($save_data);
        }else{
            $save_data = [
                'name'=>$params['name'],
                'image'=>$params['image'],
                'introduction'=>$params['introduction']??"",
                'sort'=>$params['sort']??0,
                'status'=>$params['status']??0,
                'created_at'=>time()
            ];
            Db::table('vote_person')
                ->insert($save_data);
        }
        return true;
    }
}
